<template>
  <div class="rumpage">
    <!--页面-->
    <div class="rumpage-container">
      <el-container style="height: 100%">
        <!--页头-->
        <el-header height="auto" style="padding:0">
          <div class="rumpage-title">
            <div class="rumpage-titleicon" />
            <div class="rumpage-titletex">角色管理</div>
          </div>

          <!--查询条件-->
          <div class="rumpage-query">
            <div class="rumpage-query-title">角色名</div>
            <input
              v-model="queryData.roleName"
              class="rumpage-query-input"
              type="text"
              placeholder="角色名"
            >

            <div class="rumpage-query-querybtn" @click="queryRoleEvt">查询</div>
            <div
              class="rumpage-query-addbtn"
              @click="addRoleEvt"
            >创建角色</div>
          </div>
        </el-header>

        <!--表格-->
        <el-main style="padding:0 32px;height:100%">
          <el-table
            height="100%"
            :data="roleList"
            class="rumpage-table"
            row-class-name="rumpage-table-row"
            cell-class-name="rumpage-table-cell"
            header-row-class-name="rumpage-table-header"
            header-cell-class-name="rumpage-table-headercell"
          >
            <el-table-column type="index" :index="indexMethod" label="编号" width="100" />
            <el-table-column prop="roleName" label="角色名称" />
            <el-table-column prop="remark" label="备注" />
            <el-table-column fixed="right" label="操作" width="250px">
              <template slot-scope="scope">
                <el-button type="text" @click="updateRoleEvt(scope.row)">编辑</el-button>
                <el-button type="text" @click="delRoleEvt(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>

        <!--页脚-->
        <el-footer class="el-footer" style="height:80px;padding:0 32px">
          <el-pagination
            class="pagination"
            background
            :page-size="pagination.pageSize"
            :current-page="pagination.page"
            :total="pagination.total"
            layout="prev, pager, next,sizes,jumper"
            @current-change="paginationEvt"

            @size-change="pageSizeChang"
          ><!-- -->
          </el-pagination>
        </el-footer>
      </el-container>
    </div>

    <!--添加角色-->
    <el-dialog custom-class="rumpage-formdialog" :visible.sync="addDialogVisible" width="586px">
      <div slot="title" class="rumpage-formdialogtitle">添加角色</div>

      <div class="rumpage-formdialogbody">
        <el-form ref="addRoleForm" :model="roleObj" label-width="100px">
          <el-form-item prop="roleName">
            <label slot="label" class="rumpage-formitem">角色名称</label>

            <el-input v-model="roleObj.roleName" />
            <div v-show="roleNameErrorTip !=''" class="register-errortip">{{ roleNameErrorTip }}</div>
          </el-form-item>
          <el-form-item prop="remark">
            <label slot="label" class="rumpage-formitem">备注</label>
            <el-input v-model="roleObj.remark" />
            <div v-show="remarkErrorTip !=''" class="register-errortip">{{ remarkErrorTip }}</div>
          </el-form-item>
          <el-form-item prop="menus">
            <label slot="label" class="rumpage-formitem">权限</label>
            <div class="form-tree">
              <el-scrollbar style="height:100%;">
                <el-tree
                  ref="tree"
                  :data="data"
                  show-checkbox
                  default-expand-all
                  node-key="id"
                  highlight-current
                />
              </el-scrollbar>
            </div>
          </el-form-item>

        </el-form>
      </div>

      <span slot="footer" class="rumpage-formdialogfooter">
        <div
          v-if="evnetType==1"
          class="rumpage-dialogconfirmbtn"
          type="primary"
          @click="addRoleForm"
        >添加</div>
        <div
          v-if="evnetType==2"
          class="rumpage-dialogconfirmbtn"
          type="primary"
          @click="updateRoleForm"
        >修改</div>
        <div
          class="rumpage-dialogcancalbtn"
          @click="addDialogVisible = false"
        >取消</div>
      </span>
    </el-dialog>

    <!--删除用户-->
    <el-dialog
      title="提示"
      custom-class="rumpage-tipdialog"
      :visible.sync="delDialogVisible"
      width="433px"
      :show-close="false"
    >
      <div slot="title" class="rumpage-tipdialogtitle">{{ message }}</div>
      <span slot="footer" class="rumpage-tipdialogfooter">
        <div
          class="rumpage-dialogconfirmbtn"
          type="primary"
          @click="deleteRoleForm"
        >确定</div>
        <div
          class="rumpage-dialogcancalbtn"
          @click="delDialogVisible= false"
        >取消</div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  name: 'Role',
  // components: { Pagination },
  data() {
    return {
      roleNameErrorTip: '',
      remarkErrorTip: '',
      passwordErrorTip: '',
      emailErrorTip: '',
      phoneErrorTip: '',
      addressErrorTip: '',
      pagination: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      isShow: true,
      agents: [],
      queryData: { roleName: '' },
      addDialogVisible: false,
      modifyDialogVisible: false,
      delDialogVisible: false,
      roleObj: {},
      roleList: [],
      message: '确定删除该用户?',
      evnetType: null,
      data: []
    }
  },
  mounted() {
    this.queryRoleEvt()
  },
  methods: {
    indexMethod(index) {
      return (this.pagination.page - 1) * this.pagination.pageSize + index + 1
    },
    queryRoleEvt() {
      var that = this
      var t = new Date().getTime()
      var data = {
        offset: (this.pagination.page - 1) * this.pagination.pageSize,
        limit: this.pagination.pageSize
      }

      for (const key in that.queryData) {
        if (that.queryData[key] !== '') {
          data[key] = that.queryData[key]
        }
      }

      request({
        url: `/admin/1.1/roles?t=${t}`, // ie需加时间戳
        method: 'get',
        params: data
      }).then(response => {
        that.roleList = response.data.data
        that.pagination.total = response.data.total
      }).catch(() => {

      })
    },
    paginationEvt(page) {
      this.pagination.page = page
      this.queryRoleEvt()
    },
    pageSizeChang(pageSize) {
      this.pagination.pageSize = pageSize
      this.paginationEvt(1)
    },
    enabledFormat(row, column) {
      var enabled = row[column.property]
      return enabled ? '启用' : '禁用'
    },
    categoryFormat(row, column) {
      var category = row[column.property]
      if (category === 1) {
        return '系统用户'
      } else if (category === 2) {
        return '证书订户'
      }
    },

    addRoleEvt() {
      this.roleObj = {
        roleName: '',
        remark: ''
      }
      this.evnetType = 1
      this.roleNameErrorTip = ''
      this.remarkErrorTip = ''
      this.addDialogVisible = true
      this.loadRoleTree()
    },
    createTree(pid, data) {
      var thiz = this

      var children = []

      data.forEach((item) => {
        if (item.parentId === pid) {
          var thisChildren = thiz.createTree(item.id, data)
          children.push({
            id: item.id,
            label: item.title,
            children: thisChildren
          })
        }
      })
      return children
    },
    loadRoleTree() {
      this.data = []
      request({
        url: `/admin/1.1/menus`,
        method: 'get'
      }).then(response => {
        var menus = response.data.data
        var tree = this.createTree(0, menus)
        this.data = tree

        if (this.evnetType === 2) {
          this.loadRoles(this.roleObj.id)
        }
      }).catch((e) => {
        console.error(e)
      })
    },
    loadRoles(roleId) {
      request({
        url: `/admin/1.1/roles/${roleId}/menus`, // ie需加时间戳
        method: 'get'
      }).then(response => {
        var roleList = response.data.menus
        roleList.forEach((item) => {
          this.$refs.tree.setChecked(item, true, false)
        })
      }).catch((e) => {
        console.error(e)
      })
    },
    updateRoleEvt(row) {
      this.evnetType = 2
      this.roleObj = {
        id: row.id,
        roleName: row.roleName,
        remark: row.remark
      }
      this.loadRoleTree()
      this.roleNameErrorTip = ''
      this.remarkErrorTip = ''
      this.addDialogVisible = true
    },
    delRoleEvt(row) {
      this.evnetType = 3
      this.message = '确定删除该用户？'
      this.delDialogVisible = true
      this.roleObj = {
        id: row.id
      }
    },
    roleNameVerification() {
      if (this.roleObj.roleName === '') {
        this.roleNameErrorTip = '角色名称不能为空'
        return false
      }
      this.roleNameErrorTip = ''
      return true
    },
    addRoleForm() {
      var that = this

      if (!this.roleNameVerification()) {
        return
      }
      var registerData = {
        roleName: this.roleObj.roleName,
        remark: this.roleObj.remark
      }

      request({
        url: `/admin/1.1/roles`, // ie需加时间戳
        method: 'post',
        data: registerData
      }).then(response => {
        this.patchRolePri(response.data.id)
      }).catch(error => {
        console.log(error.data)
        error.data.errors.forEach((item) => {
          if (item.desc === 'error parameter with duplicate key') {
            that.remarkErrorTip = '角色已存在'
          }
        })
      })
    },
    updateRoleForm() {
      var registerData = {}
      if (!this.roleNameVerification()) {
        return
      }
      registerData = {
        roleName: this.roleObj.roleName,
        remark: this.roleObj.remark
      }

      var id = this.roleObj.id
      request({
        url: `/admin/1.1/roles/${id}`, // ie需加时间戳
        method: 'patch',
        data: registerData
      }).then(response => {
        this.patchRolePri(id)
      }).catch(error => {
        console.log(error.data)
      })
    },
    patchRolePri(id) {
      var pris = this.$refs.tree.getCheckedKeys()

      request({
        url: `/admin/1.1/roles/${id}/menus`, // ie需加时间戳
        method: 'patch',
        data: pris
      }).then(response => {
        this.queryRoleEvt()
      }).catch(error => {
        console.log(error.data)
      })
      this.addDialogVisible = false
    },
    deleteRoleForm() {
      if (this.evnetType !== 3) {
        this.updateRoleForm()
      } else {
        var id = this.roleObj.id
        request({
          url: `/admin/1.1/roles/${id}`, // ie需加时间戳
          method: 'DELETE'
        }).then(response => {
          this.roleObj.id = response.data.id
          console.log('------------')
          this.queryRoleEvt()
        }).catch(error => {
          console.log(error.data)
        })
      }
      this.delDialogVisible = false
    }
  }
}
</script>

<!--header-->
<style>
.rumpage {
  padding: 0 24px 24px 24px;
  background: #f0f2f5;
  height: 100%;
}

.rumpage-container {
  width: 100%;
  height: 100%;
  padding: 32px 0;
  background: #ffffff;
}

.rumpage-title {
  display: flex;
  padding-bottom: 40px;
}

.rumpage-titleicon {
  height: 18px;
  width: 4px;
  background: #0057ff;
  align-self: center;
}

.rumpage-titletex {
  font-family: font-PingFangSCMedium;
  font-size: 16px;
  color: #343a40;
  letter-spacing: 0;
  line-height: 24px;
  font-weight: bold;
  align-self: center;
  margin-left: 28px;
}
</style>

<!--查询-->
<style lang='scss'>
.rumpage-query {
  padding: 0 32px 24px 32px;
  font-size: 0;
}

.rumpage-query-title {
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #343a40;
  letter-spacing: 0;
  text-align: right;
  line-height: 32px;
  display: inline-block;
}

.rumpage-query-title:not(:first-child) {
  margin-left: 24px;
}

.rumpage-query-select{
  display: inline-block;
  width: 154px;
  height: 32px;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  line-height: 32px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;

  margin-left: 16px;
}

.rumpage-query-input {
  display: inline-block;
  width: 174px;
  height: 32px;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  line-height: 32px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  // color: #c2c7d0;
  letter-spacing: 0;
  padding: 6px 8px 6px 16px;
  margin-left: 16px;
}

.rumpage-query-input:focus {
  outline: none;
  border: 1px solid #409eff;
}

.rumpage-query-querybtn {
  background: #0057ff;
  border-radius: 4px;
  width: 60px;
  height: 32px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #ffffff;
  letter-spacing: 0;
  text-align: center;
  line-height: 32px;
  display: inline-block;
  cursor: pointer;
  margin-left: 24px;
}

.rumpage-query-addbtn {
  float: right;
  background: #0057ff;
  border-radius: 4px;
  width: 70px;
  height: 32px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #ffffff;
  letter-spacing: 0;
  text-align: center;
  line-height: 32px;
  cursor: pointer;
}
</style>

<!--table、page-->
<style>
.rumpage-table {
  height: 100%;
}

.rumpage-table::before {
  background: #ffffff !important;
}

.rumpage-table .el-table__fixed-right::before {
  background: #ffffff !important;
}

.rumpage-table-header {
  font-family: font-PingFangSCSemibold;
  font-size: 14px;
  color: #343a40;
  letter-spacing: 0;
  text-align: center;
  height: 50px;
}

.rumpage-table-headercell {
  font-family: font-PingFangSCSemibold;
  font-size: 14px;
  color: #343a40;
  letter-spacing: 0;
  text-align: center !important;
  background-color: rgba(234, 234, 234, 0.15) !important;
}

.rumpage-table-row {
  height: 50px;
}

.rumpage-table-cell {
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #343a40;
  letter-spacing: 0;
  text-align: center !important;
}

.rumpage-page {
  margin-top: 45px;
  margin-right: 32px;
  text-align: right;
}
</style>

<!--form dialog-->
<style>
.rumpage-formdialog {
  background: #ffffff;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2);
  border-radius: 4px;
}

.rumpage-formdialog > .el-dialog__header {
  padding: 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.09);
}

.rumpage-formdialog > .el-dialog__body {
  padding: 0;
}

.rumpage-formdialog > .el-dialog__footer {
  padding: 24px 40px;
  border-top: 1px solid rgba(0, 0, 0, 0.09);
}

.rumpage-formdialogtitle {
  height: 56px;
  font-family: font-PingFangSCMedium;
  font-size: 16px;
  color: #343a40;
  line-height: 56px;
  font-weight: bold;
  padding-left: 40px;
}

.rumpage-formdialogbody {
  padding: 40px 40px 0px 40px;
}

.rumpage-formitem {
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #343a40;
  text-align: right;
  padding: 0 4px 0 4px;
  line-height: 40px;
}

.rumpage-formdialogfooter {
  height: 32px;
  display: block;
}
</style>

<style lang="scss" scoped>
.rumpage-formdialogbody {
  ::deep .el-radio-group {
    .is-checked {
      .is-checked {
        .el-radio__inner {
          background: #0057ff;
          border-color: #0057ff;
        }
      }
      .el-radio__label {
        color: #0057ff;
      }
    }
    .el-radio__label {
      font-family: font-PingFangSCRegular;
      font-size: 14px;
      color: #343a40;
      line-height: 20px;
    }
  }
}
</style>

<!--tip dialog-->
<style>
.rumpage-tipdialog {
  background: #ffffff;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  padding: 32px;
  top: calc(50% - 300px);
}

.rumpage-tipdialog > .el-dialog__header {
  padding: 0;
}

.rumpage-tipdialog > .el-dialog__body {
  padding: 0;
}

.rumpage-tipdialog > .el-dialog__footer {
  padding: 0;
}

.rumpage-tipdialogtitle {
  height: 24px;
  font-family: font-PingFangSCMedium;
  font-size: 16px;
  color: #343a40;
  line-height: 24px;
  font-weight: bold;
}

.rumpage-tipdialogbody {
  padding: 40px 0;
}

.rumpage-tipdialogfooter {
  height: 32px;
  display: block;
  padding-top: 12px;
}
</style>

<!--dialog btn-->
<style>
.rumpage-dialogcancalbtn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
  text-align: center;
  line-height: 32px;
  cursor: pointer;
  float: right;
  margin-right: 8px;
}

.rumpage-dialogconfirmbtn {
  width: 65px;
  height: 32px;
  background: #0057ff;
  border-radius: 4px;
  font-family: font-PingFangSCRegular;
  font-size: 14px;
  color: #ffffff;
  text-align: center;
  line-height: 32px;
  cursor: pointer;
  float: right;
}

.rumpage-dialogconfirmbtn:hover {
  background: #f2f6ff;
  border: 1px solid #3e80fc;
  color: #3e80fc;
}

.rumpage-dialogconcalbtn:hover {
  background: #f2f6ff;
  border: 1px solid #3e80fc;
  color: #3e80fc;
}

.pagination{
  float: right;
}

.register-errortip {
    position: absolute;
    z-index: 100;
    top: 40px;
    width: 320px;
    font-family: font-PingFangSCRegular;
    font-size: 12px;
    color: #f5222d;
    letter-spacing: 0;
    text-align: left;
    line-height: 24px;
    overflow: hidden; /*超出部分隐藏*/
    text-overflow: ellipsis; /* 超出部分显示省略号 */
    white-space: nowrap; /*规定段落中的文本不进行换行 */
  }
  .form-tree{
    border: 1px solid #eaeaea;
    border-radius: 4px;
    height: 140px;
    padding: 20px 0 20px 0;
  }
  .el-scrollbar__wrap{
    overflow-x: hidden;
  }
</style>

